package com.alks.function.controller.biocontrol.deptwork;

import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.deptwork.LastDetDto;
import com.alks.function.data.request.biocontrol.deptwork.LastInRequest;
import com.alks.function.data.request.biocontrol.deptwork.LastStkRequest;
import com.alks.function.service.biocontrol.deptwork.ICpLastService;
import com.alks.function.service.biocontrol.deptwork.ILastInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@ApiRestController("/bioDeptWrok")
@RoleCheck(prgIds = "140117")
@Api(value = "生控作业", tags = "生控部-生控作业-楦头入库作业")
@RequiredArgsConstructor
public class LastInController {
    private final ILastInService service;
    private final ICpLastService infoService;
    private final String slipType = "I";

    @PutMapping("/getLastInHeadQuery")
    @ApiOperation(value = "楦头入库单查询-可条件查询", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo getLastInHeadQuery(@RequestBody LastStkRequest request) {
        return service.getLastHeadQuery(request, slipType);
    }

    @PutMapping("/lastInHeadCheck")
    @ApiOperation(value = "楦头入库单检核-更新", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo lastInHeadCheck(@RequestParam @ApiParam("订单号") String slipNo) {
        return service.lastHeadCheck(slipNo, slipType);
    }

    @PostMapping("/getLastInDetQuery")
    @ApiOperation(value = "楦头入库单明细查询-条件查询", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo getLastInDetQuery(@RequestParam @ApiParam("订单号") String slipNo) {
        LastDetDto dto = service.getLastDetQuery(slipNo, slipType);
        return ResponseInfo.ok(dto);
    }

    @PutMapping("/lastInAdd")
    @ApiOperation(value = "楦头入库单创建-新增", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo lastInAdd(@RequestBody LastInRequest request) {
        /*楦头号检测*/
        if (StringUtils.isBlank(request.getLastNo())) {
            throw new ServiceErrorException("楦头号不能为空");
        }
        infoService.lastNoValid(request.getLastNo());
        String slipNo = service.lastAdd(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PutMapping("/lastInUp")
    @ApiOperation(value = "楦头入库单更新-更新", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo lastInUp(@RequestBody LastInRequest request) {
        /*楦头号检测*/
        if (StringUtils.isBlank(request.getSlipNo())) {
            throw new ServiceErrorException("订单号不能为空");
        }
        if (StringUtils.isBlank(request.getLastNo())) {
            throw new ServiceErrorException("楦头号不能为空");
        }
        infoService.lastNoValid(request.getLastNo());
        String slipNo = service.lastUp(request, slipType);
        return ResponseInfo.ok(slipNo);
    }

    @PostMapping("/lastInDelete")
    @ApiOperation(value = "整单删除-删除", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo lastInDelete(@RequestParam @ApiParam("订单号") String slipNo) {
        return service.lastDelete(slipNo, slipType);
    }

    @PutMapping("/lastInDetDelete")
    @ApiOperation(value = "楦头入库明细删除-删除", tags = "生控部-生控作业-楦头入库作业")
    public ResponseInfo lastInDetDelete(@RequestParam @ApiParam("订单号") String slipNo,
                                        @RequestParam @ApiParam("序号") String seqNo) {
        service.lastDetDelete(slipNo, seqNo, slipType);
        return ResponseInfo.ok();
    }
}
